from flask import Flask,request,render_template
app = Flask(__name__)


# 根路由 代表访问 首页
@app.route('/')
def index():
    return render_template("index.html")

@app.route('/login')
def login():
    return render_template("login.html")

@app.route('/register')
def register():
    return render_template("register.html")


@app.route('/api/login', methods=['POST'])
def api_login():
    #users是从数据库取回来的用户列表
    users = [
        {'username': "damu","userid":"2024001", 'password': "123", 'email': "damu@iflytek.com"},
        {'username': "zk", "userid": "2024002", 'password': "456", 'email': "zk@iflytek.com"},
        {'username': "hf", "userid": "2024003", 'password': "789", 'email': "hf@iflytek.com"},
        {'username': "cjy", "userid": "2024004", 'password': "111", 'email': "cjy@iflytek.com"}
    ]
    # 获取用户名和密码,检验用户名和密码,决定返回哪个页面
    # 1. 用户名和密码都能对上,返回登录成功页面
    # 2. 用户名有问题,数据库中无此用户,返回注册页面(告诉他没有这个用户,你去注册)
    # 3. 用户名没有问题,但密码有问题,返回登录失败页面(告诉他密码有问题)

    # 拿到浏览器端传过来的用户id 和 用户密码
    userid = request.form.get("userid")
    password = request.form.get("password")

    # 校验用户id 和 用户密码
    usernums = len(users)
    i = 0
    while i < usernums:
        if users[i]["userid"] == userid:
            # 有对应的用户 接下来应该查密码
            print(password == users[i]["password"])
            if password == users[i]["password"]:
                address_url = "login_success.html"
            else:
                address_url = "login_error.html"
            break
        i = i + 1

    msg = ""
    username = ""
    if i < usernums:
        username = users[i]["username"]
    if i == usernums:
        msg = "没有此用户,请注册"

    return render_template(address_url, username=username, msg=msg)


if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')